﻿using MediatR;
using MES.Server.API.Application.Command.Material_Management;
using MES.Server.API.Dto.Material_Management.OutPut;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using ReturnValue;
using System.Collections.Generic;
using System.Threading.Tasks;
using System.Threading;
using System;

namespace MES.Server.API.Controllers
{
    [Route("api/[controller]/[action]")]
    [ApiController]
    public class MaterialManagementController : ControllerBase
    {
        private readonly IMediator mediator;
        public MaterialManagementController(IMediator mediator)
        {
            this.mediator = mediator;
        }
        /// <summary>
        /// 添加物料群组
        /// </summary>
        /// <param name="request"></param>
        /// <param name="cancellationToken"></param>
        /// <returns></returns>
        [HttpPost]
        public async Task<ApiResult<int>> AddMaterialGroup(AddMaterialGroupCommand request, CancellationToken cancellationToken)
        {
            return await mediator.Send(request, cancellationToken);
        }
        /// <summary>
        /// 删除物料群组
        /// </summary>
        /// <param name="request"></param>
        /// <param name="cancellationToken"></param>
        /// <returns></returns>
        [HttpPost]
        public async Task<ApiResult<int>> DelMaterialGroup(DelMaterialGroupCommand request, CancellationToken cancellationToken)
        {
            return await mediator.Send(request, cancellationToken);
        }
        /// <summary>
        /// 物料群组查询
        /// </summary>
        /// <param name="request"></param>
        /// <param name="cancellationToken"></param>
        /// <returns></returns>
        [HttpGet]
        public Task<ApiPaging<GetMaterialGroupOutPut>> GetMaterialGroup([FromQuery]GetMaterialGroupCommand request, CancellationToken cancellationToken)
        {
            return mediator.Send(request, cancellationToken);
        }
        /// <summary>
        /// 修改物料群组信息
        /// </summary>
        /// <param name="request"></param>
        /// <param name="cancellationToken"></param>
        /// <returns></returns>
        [HttpPut]
        public async Task<ApiResult<int>> UpdateMaterialGroup(UpdateMaterialGroupCommand request, CancellationToken cancellationToken)
        {
            return await mediator.Send(request, cancellationToken);
        }
        /// <summary>
        /// 添加产品物料
        /// </summary>
        /// <param name="request"></param>
        /// <param name="cancellationToken"></param>
        /// <returns></returns>
        [HttpPost]
        public async Task<ApiResult<int>> AddProductMaterials(AddProductMaterialsCommand request, CancellationToken cancellationToken)
        {
            return await mediator.Send(request, cancellationToken);
        }
        /// <summary>
        /// 产品物料删除
        /// </summary>
        /// <param name="request"></param>
        /// <param name="cancellationToken"></param>
        /// <returns></returns>
        [HttpPost]
        public async Task<ApiResult<int>> DelProductMaterials(DelProductMaterialsCommand request, CancellationToken cancellationToken)
        {
            return await mediator.Send(request, cancellationToken);
        }
        /// <summary>
        /// 产品物料查询
        /// </summary>
        /// <param name="request"></param>
        /// <param name="cancellationToken"></param>
        /// <returns></returns>
        /// <exception cref="NotImplementedException"></exception>
        [HttpGet]
        public Task<ApiPaging<GetProductMaterialsOutPut>> GetProductMaterials([FromQuery]GetProductMaterialsCommand request, CancellationToken cancellationToken)
        {
            return mediator.Send(request, cancellationToken);
        }
        /// <summary>
        /// 修改产品物料信息
        /// </summary>
        /// <param name="request"></param>
        /// <param name="cancellationToken"></param>
        /// <returns></returns>
        [HttpPut]
        public async Task<ApiResult<int>> UpdateProductMaterials(UpdateProductMaterialsCommand request, CancellationToken cancellationToken)
        {
            return await mediator.Send(request, cancellationToken);
        }

        #region BOM管理

        /// <summary>
        /// 添加BOM
        /// </summary>
        /// <param name="request"></param>
        /// <param name="cancellationToken"></param>
        /// <returns></returns>
        [HttpPost]
        public async Task<ApiResult<int>> AddBom(AddBomCommand request, CancellationToken cancellationToken)
        {
            return await mediator.Send(request, cancellationToken);
        }

        /// <summary>
        /// 添加子级BOM
        /// </summary>
        /// <param name="request"></param>
        /// <param name="cancellationToken"></param>
        /// <returns></returns>
        [HttpPost]
        public async Task<ApiResult<int>> AddChildBom(AddChildBomCommand request, CancellationToken cancellationToken)
        {
            return await mediator.Send(request, cancellationToken);
        }

        /// <summary>
        /// BOM查询（分页）
        /// </summary>
        /// <param name="request"></param>
        /// <param name="cancellationToken"></param>
        /// <returns></returns>
        [HttpGet]
        public Task<ApiPaging<GetBomOutPut>> GetBom([FromQuery] GetBomCommand request, CancellationToken cancellationToken)
        {
            return mediator.Send(request, cancellationToken);
        }

        /// <summary>
        /// 获取BOM树形结构
        /// </summary>
        /// <param name="request"></param>
        /// <param name="cancellationToken"></param>
        /// <returns></returns>
        [HttpGet]
        public async Task<ApiResult<List<GetBomOutPut>>> GetBomTree([FromQuery] GetBomTreeCommand request, CancellationToken cancellationToken)
        {
            return await mediator.Send(request, cancellationToken);
        }

        /// <summary>
        /// 修改BOM信息
        /// </summary>
        /// <param name="request"></param>
        /// <param name="cancellationToken"></param>
        /// <returns></returns>
        [HttpPut]
        public async Task<ApiResult<int>> UpdateBom(UpdateBomCommand request, CancellationToken cancellationToken)
        {
            return await mediator.Send(request, cancellationToken);
        }

        /// <summary>
        /// 删除BOM
        /// </summary>
        /// <param name="request"></param>
        /// <param name="cancellationToken"></param>
        /// <returns></returns>
        [HttpPost]
        public async Task<ApiResult<int>> DeleteBom([FromQuery] DeleteBomCommand request, CancellationToken cancellationToken)
        {
            return await mediator.Send(request, cancellationToken);
        }

        #endregion

        #region 物料申请单管理

        /// <summary>
        /// 添加物料申请单
        /// </summary>
        /// <param name="request">物料申请单信息</param>
        /// <param name="cancellationToken">取消令牌</param>
        /// <returns>操作结果</returns>
        [HttpPost]
        public async Task<ApiResult<int>> AddMaterialAsk(AddMaterialAskCommand request, CancellationToken cancellationToken)
        {
            return await mediator.Send(request, cancellationToken);
        }

        /// <summary>
        /// 物料申请查询
        /// </summary>
        /// <param name="request">查询条件</param>
        /// <param name="cancellationToken">取消令牌</param>
        /// <returns>物料申请单列表</returns>
        [HttpGet]
        public Task<ApiPaging<GetMaterialAskOutPut>> GetMaterialAsk([FromQuery]GetMaterialAskCommand request, CancellationToken cancellationToken)
        {
            return mediator.Send(request, cancellationToken);
        }

        /// <summary>
        /// 获取待办物料申请单
        /// </summary>
        /// <param name="request">查询条件</param>
        /// <param name="cancellationToken">取消令牌</param>
        /// <returns>待办物料申请单列表</returns>
        [HttpGet]
        public async Task<ApiResult<List<GetMaterialAskOutPut>>> GetPendingMaterialAsk([FromQuery] GetPendingMaterialAskCommand request, CancellationToken cancellationToken)
        {
            return await mediator.Send(request, cancellationToken);
        }

        /// <summary>
        /// 获取物料申请单详情
        /// </summary>
        /// <param name="request">查询条件</param>
        /// <param name="cancellationToken">取消令牌</param>
        /// <returns>物料申请单详情</returns>
        [HttpGet]
        public async Task<ApiResult<GetMaterialAskOutPut>> GetMaterialAskDetail([FromQuery] GetMaterialAskDetailCommand request, CancellationToken cancellationToken)
        {
            return await mediator.Send(request, cancellationToken);
        }


        ///// <summary>
        ///// 获取待办物料申请单数量
        ///// </summary>
        ///// <param name="cancellationToken">取消令牌</param>
        ///// <returns>待办数量</returns>
        //[HttpGet]
        //public async Task<ApiResult<int>> GetPendingMaterialAskCount(CancellationToken cancellationToken)
        //{
        //    var result = new ApiResult<int>();
        //    try
        //    {
        //        // 创建查询命令
        //        var request = new GetPendingMaterialAskCommand();
                
        //        // 查询待办物料申请单
        //        var pendingResult = await mediator.Send(request, cancellationToken);
                
        //        if (pendingResult.Code == StatusCode.Success)
        //        {
        //            // 返回待办数量
        //            result.Code = StatusCode.Success;
        //            result.Message = "获取待办数量成功";
        //            result.Data = pendingResult.Data?.Count ?? 0;
        //        }
        //        else
        //        {
        //            result.Code = StatusCode.Fail;
        //            result.Message = "获取待办数量失败";
        //            result.Data = 0;
        //        }
        //    }
        //    catch (Exception ex)
        //    {
        //        result.Code = StatusCode.Fail;
        //        result.Message = $"获取待办数量异常: {ex.Message}";
        //        result.Data = 0;
        //    }
            
        //    return result;
        //}

        #endregion
    }
}